//
//  DevFormRadioField.swift
//  DevFormView
//
//  Created by dev on 15/9/18.
//  Copyright (c) 2015年 devilved. All rights reserved.
//

import UIKit

class DevFormRadioField: DevFormField {
    
    var radioImageView:UIImageView?
    
    private var checked:Bool = false
    
    override init(name: String!, label: String!) {
        super.init(name: name, label: label)
        self.controlHeight = 20
    }
    
    override func initDevFormFieldView() {
        super.initDevFormFieldView()
//        let left:CGFloat = formTableView.separatorInset.left > 0 ? formTableView.separatorInset.left : 15
        let right:CGFloat = formTableView.separatorInset.right > 0 ? formTableView.separatorInset.right : 15
        let cellHeight:CGFloat = self.height
        let radioWidth:CGFloat = self.controlHeight
        let radioHeight:CGFloat = self.controlHeight
        
        self.radioImageView = UIImageView()
        let radio:UIImageView = self.radioImageView!
        radio.frame = CGRectMake(formTableView.frame.width - right - radioWidth + 3, (cellHeight - radioHeight) / 2, radioWidth, radioHeight)
        if self.checked {
            radio.image = UIImage(named:"dev-radio-checked")
        } else {
            radio.image = UIImage(named:"dev-radio")
        }
        self.view?.addSubview(radio)
    }
    
    override func initConstraints() {
        super.initConstraints()
        let right:CGFloat = formTableView.separatorInset.right > 0 ? formTableView.separatorInset.right : 15
        let radio:UIImageView = self.radioImageView!
        let radioWidth:CGFloat = self.controlHeight
        let radioHeight:CGFloat = self.controlHeight
        let main:UIView = self.view!
        radio.translatesAutoresizingMaskIntoConstraints = false
        
        main.addConstraint(NSLayoutConstraint(item: radio, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: main, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: radio, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: right - 3))
        
        main.addConstraint(NSLayoutConstraint(item: radio, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: radioWidth))
        
        main.addConstraint(NSLayoutConstraint(item: radio, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: radioHeight))
        
    }
    
    override func setFieldValue(value: AnyObject?) {
        if let newValue:AnyObject = value {
            if let oldValue:AnyObject = self.value {
                if newValue.isEqual(oldValue) {
                    if !self.checked {
                        if let delegate = self.delegate {
                            delegate.formField(onValueChange: self, formTableView: self.formTableView, newValue: true, oldValue: false)
                        }
                        self.radioImageView?.image = UIImage(named:"dev-radio-checked")
                    }
                    self.checked = true
                    return
                }
            }
        }
        if self.checked {
            if let delegate = self.delegate {
                delegate.formField(onValueChange: self, formTableView: self.formTableView, newValue: false, oldValue: true)
            }
            self.radioImageView?.image = UIImage(named:"dev-radio")
        }
        self.checked = false
    }
    
    override func focus() {
        self.formTableView.setFormValue(self.name, value: self.value)
        super.focus()
    }
    
    override func getFieldValue() -> AnyObject? {
        if self.checked {
            return self.value
        }
        return nil
    }
    
}
